package fr.LesLoutresSauvages.laloutresportive.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLConnection;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

public class RequetesHttp {

	BufferedWriter writer = null;
	BufferedReader reader = null;
	URLConnection connexion = null;
	String urlBase = null;

	public RequetesHttp() {
		urlBase = "http://live-score.sqli.cloudbees.net/livescore/";
	}

	private String requeteGET(String complURI) {
		String reponse = "";
		String ligne = "";

		DefaultHttpClient client = new DefaultHttpClient();
		HttpGet req = new HttpGet(urlBase + complURI);
		try {
			HttpResponse rep = client.execute(req);
			InputStream in = rep.getEntity().getContent();
			reader = new BufferedReader(new InputStreamReader(in));
			while ((ligne = reader.readLine()) != null) {
				reponse += ligne + "\n";
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return reponse;
	}
		
	private String requetePOST(String complURI, String json) {
		String reponse = "";
		String ligne = "";

		DefaultHttpClient client = new DefaultHttpClient();
		HttpPost req = new HttpPost(urlBase + complURI);
		
		StringEntity param;
		try {
			param = new StringEntity(json);
			req.setEntity(param);
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		try {
			HttpResponse rep = client.execute(req);
			InputStream in = rep.getEntity().getContent();
			reader = new BufferedReader(new InputStreamReader(in));
			while ((ligne = reader.readLine()) != null) {
				reponse += ligne + "\n";
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return reponse;
	}

	private String requetePUT(String complURI, String json) {
		String reponse = "";
		String ligne = "";

		DefaultHttpClient client = new DefaultHttpClient();
		HttpPut req = new HttpPut(urlBase + complURI);
		
		StringEntity param;
		try {
			param = new StringEntity(json);
			req.setEntity(param);
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		try {
			HttpResponse rep = client.execute(req);
			InputStream in = rep.getEntity().getContent();
			reader = new BufferedReader(new InputStreamReader(in));
			while ((ligne = reader.readLine()) != null) {
				reponse += ligne + "\n";
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return reponse;
	}
	
	private String requeteDELETE(String complURI) {
		String reponse = "";
		String ligne = "";

		DefaultHttpClient client = new DefaultHttpClient();
		HttpDelete req = new HttpDelete(urlBase + complURI);
		try {
			HttpResponse rep = client.execute(req);
			InputStream in = rep.getEntity().getContent();
			reader = new BufferedReader(new InputStreamReader(in));
			while ((ligne = reader.readLine()) != null) {
				reponse += ligne + "\n";
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return reponse;
	}

	public String getListeDepartements() {
		return requeteGET("departements");
	}

	public String getListeSports() {
		return requeteGET("sports");
	}

	public String getDetailsSport(int idSport) {
		return requeteGET("sport/" + idSport);
	}

	public String getListeCompetitions() {
		return requeteGET("competitions");
	}

	public String getDetailsCompetition(int idSport) {
		return requeteGET("competition/" + idSport);
	}

	public String getListeLivesParDepartementEtSport(int idDepartement,
			int idSport) {
		return requeteGET("livesByDepartementAndSport/" + idDepartement + "/"
				+ idSport);
	}

	public String getListeLivesParCompetition(int idCompetition) {
		return requeteGET("livesByCompetition/" + idCompetition);
	}

	public String getListeLivesParDate(int idDate) {
		return requeteGET("livesByDate/" + idDate);
	}

	public String getListeLives() {
		return requeteGET("lives");
	}

	public String getDetailsLive(int idLive) {
		return requeteGET("lives/" + idLive);
	}

	public String postCreationLive(LiveCreation live) {
		return requetePOST("live", live.toJSON());
	}

	public void putMajScore(int idLive, int scoreEquipe1, int scoreEquipe2) {
		requetePUT("live/"+idLive+"/score", "{\"scoreEquipe1\":"+scoreEquipe1+",\"scoreEquipe2\":"+scoreEquipe2+"}");

	}

	public void putAjouterEvenement(int idLive, EvenementCreation evenement) {
		requetePUT("live/"+idLive+"/evenement", "{\"evenement\":"+evenement.getCommentaire()+"\"}");
	}

	public void deleteLive(int idLive) {
		requeteDELETE("live/" + idLive);
	}
}
